package com.example.noteapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.util.Date;

public class EditNoteActivity extends AppCompatActivity {
    private EditText noteTitleEdit;
    private EditText noteContentEdit;
    private Note currentNote;
    private boolean isNewNote = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_note_activity);

        // 初始化UI组件
        noteTitleEdit = findViewById(R.id.note_title_edit);
        noteContentEdit = findViewById(R.id.note_content_edit);
        Toolbar toolbar = findViewById(R.id.toolbar);

        // 设置Toolbar
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 检查是否有笔记数据传入
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("note")) {
            currentNote = (Note) intent.getSerializableExtra("note");
            isNewNote = false;
            // 填充笔记数据到编辑框
            noteTitleEdit.setText(currentNote.getTitle());
            noteContentEdit.setText(currentNote.getContent());
            // 设置标题为"编辑笔记"
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(R.string.edit_note);
            }
        } else {
            // 新建笔记
            currentNote = new Note();
            // 设置标题为"添加笔记"
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(R.string.add_note);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.edit_note_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            // 点击返回按钮
            finish();
            return true;
        } else if (item.getItemId() == R.id.action_save) {
            // 点击保存按钮
            saveNote();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveNote() {
        String title = noteTitleEdit.getText().toString().trim();
        String content = noteContentEdit.getText().toString().trim();

        // 验证输入
        if (title.isEmpty() && content.isEmpty()) {
            Toast.makeText(this, "笔记标题和内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 更新笔记数据
        currentNote.setTitle(title);
        currentNote.setContent(content);
        currentNote.setTimestamp(new Date().getTime());

        // 返回结果给MainActivity
        Intent resultIntent = new Intent();
        resultIntent.putExtra("note", currentNote);
        resultIntent.putExtra("is_new", isNewNote);
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    @Override
    public void onBackPressed() {
        // 处理返回键，检查是否有未保存的更改
        String title = noteTitleEdit.getText().toString().trim();
        String content = noteContentEdit.getText().toString().trim();

        // 如果是新建笔记且有内容，或者是编辑笔记且内容有变化，则提示保存
        if ((isNewNote && !title.isEmpty() && !content.isEmpty()) || 
                (!isNewNote && (!title.equals(currentNote.getTitle()) || !content.equals(currentNote.getContent())))) {
            // 这里可以添加一个保存提示对话框，为了简洁，我们直接保存
            saveNote();
        } else {
            super.onBackPressed();
        }
    }
}